package student.action;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dao.HomeworkMagDao;
import dao.impl.HomeworkMagImpl;
import domain.Homework;
import domain.Student;


/**
 * 
 * Jun 4, 2009
 *
 * @author Fabian
 *
 */
public class StudentHomeworkAction extends DispatchAction {
	/**
	 * 显示一页的Homework
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Student stu = (Student)request.getSession().getAttribute("user");
		int stu_id = stu.getId();
		
		String flag = request.getParameter("flag");
		
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		
		List<Homework> list = null;
		int totalCount = 0;
		
		HomeworkMagDao hmd = new HomeworkMagImpl();
		
		String resultArray = "";
		
		if ("book".equals(flag)) {
			list = hmd.getStuPageBookHomework(start, limit, stu_id);
			totalCount = hmd.getStuBookTotalCount(stu_id);			
			
			for (Iterator<Homework> it = list.iterator(); it.hasNext(); ) {
				Homework h = it.next();
				Date pub_time = h.getPub_time();
				Date sub_time = h.getSub_time();				
				long p = pub_time.getTime();
				long e = sub_time.getTime();
				
				Date now = new Date();
				long now_l = now.getTime();
				
				String status = "进行中...";
				if (now_l > e) {
					status = "已经过期";
				}
				
				resultArray = resultArray + "{id: " + h.getId() + ", status: '" + status + "', title:'" + h.getTitle() + "', content:'" + h.getContent() + 
											"', pub_user:'" + h.getPub_user() + "', pub_time: new Date(" + p + "), sub_time: new Date(" + e + "), if_done:'" + 
											h.getIf_done() + "', need_count:" + h.getNeed_count() + ", sub_count:" + 
											h.getSub_count() + ", correct_count:" + h.getCorrect_count() + "}";
				if (it.hasNext()) {
					resultArray = resultArray + ",";
				}			
			}
		} else if ("good".equals(flag)) {
			list = hmd.getStuPageGoodHomework(start, limit, stu_id);
			totalCount = hmd.getStuGoodTotalCount(stu_id);			
			
			for (Iterator<Homework> it = list.iterator(); it.hasNext(); ) {
				
				Homework h = it.next();
				
				Date pub_time = h.getPub_time();
				Date sub_time = h.getSub_time();				
				long p = pub_time.getTime();
				long e = sub_time.getTime();
				
				Date now = new Date();
				long now_l = now.getTime();
				
				String status = "进行中...";
				if (now_l > e) {
					status = "已经过期";
				}			
				
				if ("swf".equals(h.getFormat())) {
					resultArray = resultArray + "{id: " + h.getId() + ", status: '" + status + "', title:'" + h.getTitle() + "', content:'" + h.getContent() + "', format: '" + h.getFormat() + 
											"',size: '" + h.getSize() + "', pub_user:'" + h.getPub_user() + "', pub_time: new Date(" + p + "), sub_time: new Date(" + e + "), if_done:'" + 
											h.getIf_done() + "', need_count:" + h.getNeed_count() + ", sub_count:" + 
											h.getSub_count() + ", correct_count:" + h.getCorrect_count() + ", operate: '" +
											"<a href=\"jsp/tch/see_online.jsp?id=" + h.getId() + "&path=" + h.getPath() + "\" target=\"_blank\"><img src=\"images/main/book_open.png\" alt=\"在线浏览\"/></a>" + 
											"&nbsp;&nbsp;&nbsp;&nbsp;" +
											"<a href=\"jsp/tch/download.jsp?path=" + h.getPath() + "\" target=\"_blank\"><img src=\"images/main/download.png\" alt=\"下载\"/></a>'}";
				} else {
					resultArray = resultArray + "{id: " + h.getId() + ", status: '" + status + "', title:'" + h.getTitle() + "', content:'" + h.getContent() + "', format: '" + h.getFormat() + 
											"',size: '" + h.getSize() + "', pub_user:'" + h.getPub_user() + "', pub_time: new Date(" + p + "), sub_time: new Date(" + e + "), if_done:'" + 
											h.getIf_done() + "', need_count:" + h.getNeed_count() + ", sub_count:" + 
											h.getSub_count() + ", correct_count:" + h.getCorrect_count() + ", operate: '" +
											"<a href=\"jsp/tch/download.jsp?path=" + h.getPath() + "\" target=\"_blank\"><img src=\"images/main/download.png\" alt=\"下载\"/></a>'}";
				}
				if (it.hasNext()) {
					resultArray = resultArray + ",";
				}			
			}
		}
		
		String jsonStr = "";
		jsonStr = "{totalProperty: " + totalCount + ", root: [ " + resultArray + " ]}";
		
		request.setAttribute("result", jsonStr);
				
		return mapping.findForward("show_result");
	}
}
